package com.yogee.topseller.activity.my;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.persenter.UploadVoucherPersenter;
import com.yogee.topseller.activity.persenter.UploadVoucherPimpl;
import com.yogee.topseller.activity.view.UploadVoucherVimpl;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.NeedPermissions;
import com.yogee.topseller.view.VoucherPop;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UploadVoucherActivity extends HttpActivity implements VoucherPop.OnIdCardPopClickListener, UploadVoucherVimpl {

    @BindView(R.id.upload_voucher_title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.upload_voucher_bg_v)
    View uploadVoucherBgV;
    @BindView(R.id.upload_voucher_remittance_iv)
    ImageView remittanceIv;
    @BindView(R.id.upload_voucher_positive_iv)
    ImageView positiveIv;
    @BindView(R.id.upload_voucher_back_iv)
    ImageView backIv;
    @BindView(R.id.upload_voucher_card_positive_iv)
    ImageView cardPositiveIv;
    @BindView(R.id.upload_voucher_card_back_iv)
    ImageView cardBackIv;
    @BindView(R.id.upload_voucher_remittance_c_iv)
    ImageView uploadVoucherRemittanceCIv;
    @BindView(R.id.upload_voucher_positive_c_iv)
    ImageView uploadVoucherPositiveCIv;
    @BindView(R.id.upload_voucher_back_c_iv)
    ImageView uploadVoucherBackCIv;
    @BindView(R.id.upload_voucher_card_positive_c_iv)
    ImageView uploadVoucherCardPositiveCIv;
    @BindView(R.id.upload_voucher_card_back_c_iv)
    ImageView uploadVoucherCardBackCIv;
    private int GALLERY_RESULT = 150;
    private int CAMERA_RESULT = 100;
    private File remittanceFile, positiveFile, backFile, cardPositiveFile, cardBackFile;
    private String type;
    private UploadVoucherPimpl p;
    private String orderId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_upload_voucher;
    }

    @Override
    protected void initView() {
        titleLayout.setActivity(this);
        titleLayout.setTitle("上传凭证");
        orderId = getIntent().getStringExtra("orderId");
        titleLayout.setTitleTvColor(getResources().getColor(R.color.white));
        p = new UploadVoucherPersenter(this);
    }

    @OnClick({R.id.upload_voucher_remittance_rl, R.id.upload_voucher_positive_rl, R.id.upload_voucher_back_rl, R.id.upload_voucher_card_positive_rl, R.id.upload_voucher_card_back_rl, R.id.upload_voucher_sure_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.upload_voucher_remittance_rl://上传汇款凭条
                type = "1";
                replacePhoroOrCorver();
                break;
            case R.id.upload_voucher_positive_rl://上传身份证正面
                type = "2";
                replacePhoroOrCorver();
                break;
            case R.id.upload_voucher_back_rl://上传身份证背面
                type = "3";
                replacePhoroOrCorver();
                break;
            case R.id.upload_voucher_card_positive_rl://上传银行卡正面
                type = "4";
                replacePhoroOrCorver();
                break;
            case R.id.upload_voucher_card_back_rl://上传银行卡背面
                type = "5";
                replacePhoroOrCorver();
                break;
            case R.id.upload_voucher_sure_tv://确定
                if (remittanceFile != null) {
                    if (positiveFile != null) {
                        if (backFile != null) {
                            if (cardPositiveFile != null) {
                                if (cardBackIv != null) {
                                    p.credentials(orderId, AppUtil.getUserId(this), remittanceFile, positiveFile, backFile, cardPositiveFile, cardBackFile);
                                } else {
                                    ToastUtils.showToast(this, "请完成银行卡背面照");
                                }
                            } else {
                                ToastUtils.showToast(this, "请完成银行卡正面照");
                            }
                        } else {
                            ToastUtils.showToast(this, "请完成身份证背面照");
                        }
                    } else {
                        ToastUtils.showToast(this, "请完成身份证正面照");
                    }
                } else {
                    ToastUtils.showToast(this, "请完成汇款凭条");
                }
                break;
        }
    }

    public void replacePhoroOrCorver() {
        List<String> permissions = new NeedPermissions(this).cameraPermissions();
        if (permissions.size() > 0) {
            String[] permission = new String[permissions.size()];
            for (int i = 0; i < permissions.size(); i++) {
                permission[i] = permissions.get(i);
            }
            ActivityCompat.requestPermissions(this, permission, 1);
        } else {
            new VoucherPop(uploadVoucherBgV, this, this).idCardPop();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "部分所需权限未开启，将影响功能正常使用，请开启权限！", Toast.LENGTH_LONG).show();
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT && resultCode == RESULT_OK) {
            switch (type) {
                case "1":
                    setViewImage(remittanceFile.getPath());
                    break;
                case "2":
                    setViewImage(positiveFile.getPath());
                    break;
                case "3":
                    setViewImage(backFile.getPath());
                    break;
                case "4":
                    setViewImage(cardPositiveFile.getPath());
                    break;
                case "5":
                    setViewImage(cardBackFile.getPath());
                    break;
            }
        }
        if (requestCode == GALLERY_RESULT && resultCode == RESULT_OK) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            setViewImage(picturePath);
        }
    }

    @Override
    public void onCameraClick() {
        p.toCamera(this, type);
    }

    @Override
    public void onAlbumClick() {
        p.toGallery(type);
    }

    @Override
    public void toCameraForResult(Intent intent, String type, File file) {
        switch (type) {
            case "1":
                remittanceFile = file;
                break;
            case "2":
                positiveFile = file;
                break;
            case "3":
                backFile = file;
                break;
            case "4":
                cardPositiveFile = file;
                break;
            case "5":
                cardBackFile = file;
                break;
        }
        startActivityForResult(intent, CAMERA_RESULT);
    }

    @Override
    public void toGalleryForResult(Intent intent, String type) {
        startActivityForResult(intent, GALLERY_RESULT);
    }

    @Override
    public void credentialsSuccess() {
        ToastUtils.showToast(this, "上传凭证成功");
        finish();
    }

    public void setViewImage(String path) {
        switch (type) {
            case "1":
                if (remittanceFile == null) {
                    remittanceFile = new File(path);
                }
                Glide.with(this).load(path).into(remittanceIv);
                uploadVoucherRemittanceCIv.setVisibility(View.GONE);
                break;
            case "2":
                if (positiveFile == null) {
                    positiveFile = new File(path);
                }
                Glide.with(this).load(path).into(positiveIv);
                uploadVoucherPositiveCIv.setVisibility(View.GONE);
                break;
            case "3":
                if (backFile == null) {
                    backFile = new File(path);
                }
                Glide.with(this).load(path).into(backIv);
                uploadVoucherBackCIv.setVisibility(View.GONE);
                break;
            case "4":
                if (cardPositiveFile == null) {
                    cardPositiveFile = new File(path);
                }
                Glide.with(this).load(path).into(cardPositiveIv);
                uploadVoucherCardPositiveCIv.setVisibility(View.GONE);
                break;
            case "5":
                if (cardBackFile == null) {
                    cardBackFile = new File(path);
                }
                Glide.with(this).load(path).into(cardBackIv);
                uploadVoucherCardBackCIv.setVisibility(View.GONE);
                break;
        }
    }

}